package com.xx.questionnaire.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 过滤request时的字符编码
 * @author Administrator
 *
 */
public final class EncodingFilter implements Filter {
	protected String encoding;
	/**
	 * 初始字符编码的变量名
	 */
	private String initEncodingName = "encoding";
	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	public void doFilter(ServletRequest RequestS, ServletResponse ResponseS,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		if(encoding!=null){
			RequestS.setCharacterEncoding(encoding);
			System.out.println("统一将编码类型设为："+encoding);
		}
		chain.doFilter(RequestS, ResponseS);
	}

	public void init(FilterConfig config) throws ServletException {
	 this.encoding = config.getInitParameter(this.getInitEncodingName());
	 System.out.println("初始化编码："+this.encoding);

	}

	public final String getInitEncodingName() {
		return initEncodingName;
	}

}
